package org.example.library;

import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;


public class ServidorPHP implements Servidor{
    Httppostaux post = new Httppostaux();
    String IP_Server = "10.0.2.2";
    String URL_connect = "http://"+IP_Server+"/CheckAndEat/utilidades/";
    ArrayList<HashMap<String, String>> resultado = new ArrayList<HashMap<String, String>>();
    String Mail_comensal,Pass_comensal,Nombre_comensal,Apellido_comensal,Tlf_comensal, ID_Reserva;
    String ID_restaurante,Fecha_reserva,Numero_comensales,ID_Reg;
      
    public void SetIDRestaurante(String id){this.ID_restaurante = id;}
    public void SetMailComensal(String mail){this.Mail_comensal = mail;}
    public void SetPassComensal(String pass){this.Pass_comensal = pass;}
    public void SetNombreComensal(String nombre){this.Nombre_comensal = nombre;}
    public void SetApellidoComensal(String apellido){this.Apellido_comensal = apellido;}
    public void SetTlfComensal(String tlf){this.Tlf_comensal = tlf;}
    public void SetFechaReserva(String fecha){this.Fecha_reserva = fecha;}
    public void SetNumeroComensal(String numero){this.Numero_comensales = numero;}
    public void SetIDReserva(String id){this.ID_Reserva = id;}
    public void SetIDReg(String id){this.ID_Reg = id;}
    
    
    public void CancelarReserva() {
            ArrayList<NameValuePair> postparameters2send= new ArrayList<NameValuePair>();
            postparameters2send.add(new BasicNameValuePair("id", this.ID_Reserva));
            JSONArray jdata = post.getserverdata(postparameters2send, URL_connect+"CancelarOfertaComensal.php");
            
            HashMap<String,String> res = new HashMap();
        
            try{
                res.put("estado", jdata.getJSONObject(0).getInt("logstatus")+"");
                resultado.add(res);
            }catch(Exception e){
                resultado = null;
            }
    }

    public void RegistrarComensal() {
        ArrayList<NameValuePair> postparameters2send= new ArrayList<NameValuePair>();
        postparameters2send.add(new BasicNameValuePair("usuario", Mail_comensal));
        postparameters2send.add(new BasicNameValuePair("password", Pass_comensal));
        postparameters2send.add(new BasicNameValuePair("nombre", Nombre_comensal));
        postparameters2send.add(new BasicNameValuePair("apellido", Apellido_comensal));
        postparameters2send.add(new BasicNameValuePair("telefono", Tlf_comensal));
        
        JSONArray jdata = post.getserverdata(postparameters2send, URL_connect+"RegistrarComensal.php");
        
        HashMap<String,String> res = new HashMap();
        
        try{
            res.put("estado", jdata.getJSONObject(0).getInt("logstatus")+"");
            resultado.add(res);
        }catch(Exception e){
            resultado = null;
        }
    }

    public void GetDetalles() {
            ArrayList<NameValuePair> postparameters2send= new ArrayList<NameValuePair>();
            postparameters2send.add(new BasicNameValuePair("id", this.ID_restaurante));
            JSONArray jdata = post.getserverdata(postparameters2send, URL_connect+"DetalleRestaurante.php");
            
            HashMap<String,String> res = new HashMap();
            
            try{
                res.put("nombre", jdata.getJSONArray(0).get(2).toString());
                res.put("descripcion",jdata.getJSONArray(0).get(8).toString());
                res.put("tlf",jdata.getJSONArray(0).get(3).toString());
                res.put("direccion",jdata.getJSONArray(0).get(6).toString());
                resultado.add(res);
            }catch(Exception e){
                resultado = null;
            }
    }

    public void GetReservas() {
            ArrayList<NameValuePair> postparameters2send= new ArrayList<NameValuePair>();
            postparameters2send.add(new BasicNameValuePair("id", this.Mail_comensal));
            JSONArray jdata = post.getserverdata(postparameters2send, URL_connect+"ReservasUsuario.php");
            
            for(int i=0;i<jdata.length()-1;i++){
	    	try{
	    		HashMap<String,String> datosEvento=new HashMap<String, String>();
	    		datosEvento.put("boton", jdata.getJSONArray(i).get(0).toString());
	    		datosEvento.put("restaurante", jdata.getJSONArray(i).get(1).toString());
	    		datosEvento.put("personas", jdata.getJSONArray(i).get(2).toString());
	    		datosEvento.put("fecha", jdata.getJSONArray(i).get(3).toString());
                        datosEvento.put("estado", jdata.getJSONArray(i).get(4).toString());
                        resultado.add(datosEvento);
	    	}
	    	catch(Exception ex){
                    resultado = null;
                }
                
            }
    }

    public void GetRestaurantes() {
        ArrayList<NameValuePair> postparameters2send= new ArrayList<NameValuePair>();
        JSONArray jdata = post.getserverdata(postparameters2send, URL_connect+"ObtenerRestaurantes.php");
        
        HashMap<String,String> res;
        
        try{
            for(int i=0;i<jdata.length()-1;i++){
                res = new HashMap();
                res.put("nombre", jdata.getJSONArray(i).get(2).toString());
                res.put("direccion", jdata.getJSONArray(i).get(6).toString());
                res.put("coordx", jdata.getJSONArray(i).get(4).toString());
                res.put("coordy", jdata.getJSONArray(i).get(5).toString());
                res.put("tipo", jdata.getJSONArray(i).get(7).toString());
                res.put("id", jdata.getJSONArray(i).get(0).toString());
                resultado.add(res);
            }
        }catch(Exception e){
            resultado = null;
        }
    }

    public void HacerReserva() {
        ArrayList<NameValuePair> postparameters2send= new ArrayList<NameValuePair>();
        postparameters2send.add(new BasicNameValuePair("user", Mail_comensal));
        postparameters2send.add(new BasicNameValuePair("idrest", ID_restaurante+""));
        postparameters2send.add(new BasicNameValuePair("fecha", Fecha_reserva));
        postparameters2send.add(new BasicNameValuePair("ncomensales", Numero_comensales+""));
        JSONArray jdata = post.getserverdata(postparameters2send, URL_connect+"EnviarOferta.php");
        
        HashMap<String,String> res = new HashMap();
        
        try{
            res.put("estado", jdata.getJSONObject(0).getInt("logstatus")+"");
            resultado.add(res);
        }catch(Exception e){
            resultado = null;
        }
    }

    public void GetUsuarios() {
        ArrayList<NameValuePair> postparameters2send= new ArrayList<NameValuePair>();
        postparameters2send.add(new BasicNameValuePair("usuario", Mail_comensal));
        postparameters2send.add(new BasicNameValuePair("password", Pass_comensal));
        JSONArray jdata = post.getserverdata(postparameters2send, URL_connect+"AccederComensal.php");
        
        HashMap<String,String> res = new HashMap();
        
        try{
            res.put("estado", jdata.getJSONObject(0).getInt("id")+"");
            resultado.add(res);
        }catch(Exception e){
            resultado = null;
        }
    }

    public ArrayList<HashMap<String, String>> GetResultado() {return this.resultado;}
    
    public void RegistrarUsuario(){
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("user",Mail_comensal));
        nameValuePairs.add(new BasicNameValuePair("gcmcode",ID_Reg));
        
        HttpClient httpclient = new DefaultHttpClient();
        Httppostaux httppost = new Httppostaux();
        
        if(nameValuePairs != null)
            httppost.getserverdata(nameValuePairs, "http://10.0.2.2/CheckAndEat/utilidades/ActualizarGCM.php");
    }
}
